package com.bwsoft.api.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.module.SimpleAbstractTypeResolver;
import org.springframework.stereotype.Service;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.admin.domain.QualityControl;
import com.bwsoft.admin.domain.Report;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.api.service.ReportService;
import com.bwsoft.api.util.Constant;
import com.bwsoft.util.Base64Util;
import com.bwsoft.util.DesUtil;
@Service(ReportService.NAME)
public class ReportServiceImpl extends BaseServiceImpl implements ReportService{
	private static Logger LOG = Logger.getLogger(ReportServiceImpl.class);

	
	/**
	 * 对应企业的报告或者是
	 * 
	 */
	@Override
	public ResponsePojo getList(Integer userid, String search, Integer page) {
		
		ResponsePojo rp = new ResponsePojo();
		// 判断验证码是否正确
		try {
			
			
			LOG.info("开始查询用户编号为："+userid+"的用户");
			Account account= accountDao.findById(userid);
			
			if (account == null) {
				rp.setErrcode(1);
				rp.setErrmsg("没有 用户编号为："+userid+"的用户");
				return rp;
			}
			
			String hql="from Report as r where (r.danwei.accountId=? or r.shangchuan.accountId=?) and 1=1 ";
			if(search!=null && !search.trim().equals("")){
				hql+=" and r.sampleName like '%"+search+"%'";
			}
			hql+=" order by r.addTime desc";
			LOG.info("组装的HQL ：" +hql);
			
			
			List<Report> totalCount=reportDao.findByHql(hql.toString(),  userid,userid);
			List<Report> reports=reportDao.findPage(hql.toString(), Constant.PAGE_COUNT , (page-1)*Constant.PAGE_COUNT, userid,userid);
			LOG.info("按照条件可以查出"+totalCount.size()+"条数据");
			LOG.info("查询出"+reports.size()+"条数据");
			
			
			
			Map<String ,Object> returnMap=new HashMap();
			returnMap.put("pageNow", page);
			returnMap.put("totalPage", totalCount.size()/Constant.PAGE_COUNT+1);
			returnMap.put("report", createReport(reports));
			rp.setErrcode(0);
			rp.setErrmsg("操作成功！");
			rp.setData(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.info("异常" + e);
			rp.setErrcode(50000);
			rp.setErrmsg("服务器异常");
			rp.setData(e.toString());
			return rp;
		}
		return rp;
		
	}
	
	
	private List<Map<String,Object>> createReport(List<Report> reports){
		List<Map<String,Object>> returnMap=new ArrayList<Map<String,Object>>();
		
		for (Report report : reports) {
			
			Map<String,Object> map=new HashMap<String, Object>();
			
			map.put("id", report.getReportId());
			map.put("uploader", report.getShangchuan().getCompName());
			map.put("compName", report.getDanwei().getCompName());
			map.put("reportName", report.getReportName());
			map.put("date", new SimpleDateFormat("yyyy-MM-dd").format(report.getAddTime()));
			try {
				map.put("fileUrl", "/api/jsp/pdf.jsp?r="+Base64Util.getBase64(DesUtil.encrypt("fileid="+report.getFile().getId(), Constant.KEY_DES).getBytes("utf-8")));
			} catch (Exception e) {
				e.printStackTrace();
				LOG.info("组装文件路径异常"+e);
				map.put("fileUrl", "/api/jsp/pdferror.jsp?");
			}
			returnMap.add(map);
		}
		
		return returnMap;
	}

}
